package com.example.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.DepartmentMapper;
import com.example.pojo.entity.Department;
import com.example.pojo.entity.Project;
import com.example.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;


/**
 * TODO
 *
 * @author ding
 * @since 2024/6/13
 */
@Service
@DS("master")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    UserDepartmentServiceImpl userDepartmentServiceImpl;

    @Autowired
    ProjectServiceImpl projectService;

    /**
     * 分页查询部门信息
     */
    public Page<Department> selectPage(String name,String projectName, String order, Integer pageNum, Integer pageSize) {
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(name),"name",name)
                    .eq("is_deleted", 0);
        Project project = projectService.selectByName(projectName);
        if (project != null){
            queryWrapper.eq(StrUtil.isNotBlank(projectName),"project_id",project.getId());
        }
        if (order != null){
            if (order.equals("ascend")){

                queryWrapper.orderByAsc("num");
            }
            if (order.equals("descend")){
                queryWrapper.orderByDesc("num");
            }
        }
        Page<Department> departmentPage = departmentMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        List<Department> records = departmentPage.getRecords();
        for (Department record : records) {
            record.setNum(userDepartmentServiceImpl.countById(record.getId()));
            record.setProjectName(projectService.getById(record.getProjectId()).getName());
        }
        departmentPage.setRecords(records);

        return departmentPage;
    }

    /**
     * 添加新部门
     */
    public void addDepartment(Department department) {
        department.setSetTime(LocalDate.now());
        //设置新建部门中的人数为:0
        department.setNum(0);
        department.setIsDeleted(0);
        department.setVersion(0);
        departmentMapper.insert(department);
    }

    /**
     * 根据id获取部门信息
     */
    public Department selectById(int id){
        /*QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return getOne(queryWrapper);*/
        return departmentMapper.selectById(id);
    }

    /**
     * 根据id删除部门信息
     */
    public void deleteById(Integer id) {
        Department department = selectById(id);
        department.setIsDeleted(1);
        update(department);
        userDepartmentServiceImpl.deleteByDepartmentId(id);
    }

    //修改部门信息
    public void update(Department department){
        departmentMapper.updateById(department);
    }
}
